/**
 * @author ChaP
 * @date 2019/06/30
 * @describe:
 */
package CodingTest.AC20190630;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**leetcode - 515
 * 您需要在二叉树的每一行中找到最大的值。

 示例：

 输入:

 1
 / \
 3   2
 / \   \
 5   3   9

 输出: [1, 3, 9]

 *
 */
public class largestValues {
    class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x){val=x;}
    }
    public List<Integer> largestValue(TreeNode root){
        Queue<TreeNode> q = new LinkedList<>();
        List<Integer> res = new ArrayList<>();
        if(root == null) return res;
        q.offer(root);
        int max ;
        while(!q.isEmpty()){
            int len = q.size();
            max = Integer.MIN_VALUE;
            for(int i = 0;i<len;i++){
                TreeNode temp = q.poll();
                max = Math.max(max,temp.val);
                if(temp.left != null) q.offer(temp.left);
                if(temp.right != null) q.offer(temp.right);
            }
            res.add(max);
        }
        return  res;
    }
}
